/**
 * 
 */
package com.oscarmovie.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Movie;
import com.oscarmovie.model.Statistics;
import com.oscarmovie.model.Mpr;
import com.oscarmovie.model.Paging;
import com.oscarmovie.model.Person;
import com.oscarmovie.service.MovieService;
import com.oscarmovie.service.PersonService;

/**
 * @author zhengda
 *
 */
public class AjaxIndexAction extends ActionSupport {
	
	private MovieService movieService;
	private PersonService personService;	
	
	private Movie movie;
	private List<Movie> movies = new ArrayList();
	
	private Set<Movie> relevantMovies = new HashSet();
	
	private static Map<Integer, Movie> movieMap = new ConcurrentHashMap<Integer, Movie>();
		
	private int movieId;
	private int calendarId;
	private int categoryId;
	private int countryId;
	private int personId;
	private int awardId;
	private int type;	
	
	private String roleId;
			
	private Paging paging = new Paging(10);
	
	public String execute() throws Exception {						
		movies = movieService.list(calendarId, categoryId, countryId, awardId, personId, type, paging);		
		return Action.SUCCESS;
	}
	
	public String load() throws Exception {
		movie = AjaxIndexAction.movieMap.get(movieId);
		if ( movie == null){
			movie = movieService.load(movieId);
			movieMap.put(movieId, movie);
		} 
		
		return Constants.DETAIL;
	}
	
	public String getRelevantMovie() throws Exception{
		Movie movie = movieService.load(movieId);
		for (Mpr movieMpr : movie.getMprs()){
			if( containsRoleId(movieMpr.getRole().getId())){				
				for (Mpr personMpr : movieMpr.getPerson().getMprs()){
					Movie movie2 = personMpr.getMovie();					
					if (movie2.getId() != movie.getId()) {
						relevantMovies.add(movie2);
					}
				}
			}
		}
		
		return Action.SUCCESS;
	}
	
	public String getRelevantMovieByPerson() throws Exception{
		Person person = personService.load(personId);							
				for (Mpr personMpr : person.getMprs()){
					Movie movie2 = personMpr.getMovie();										
				  relevantMovies.add(movie2);					
				}
						
		return Action.SUCCESS;
	}
	
	private boolean containsRoleId(int role){
		String[] roleIds = roleId.split(",");
		for (String id : roleIds){			
			if(Integer.valueOf(id) == role) return true;
		}
		
		return false;
	}
	
	/**
	 * @return the roleId
	 */
	public String getRoleId() {
		return roleId;
	}

	/**
	 * @param roleId the roleId to set
	 */
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	
	

	/**
	 * @return the relevantMovies
	 */
	public Set<Movie> getRelevantMovies() {
		return relevantMovies;
	}

	/**
	 * @param relevantMovies the relevantMovies to set
	 */
	public void setRelevantMovies(Set<Movie> relevantMovies) {
		this.relevantMovies = relevantMovies;
	}

	/**
	 * @return the personService
	 */
	public PersonService getPersonService() {
		return personService;
	}

	/**
	 * @param personService the personService to set
	 */
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	/**
	 * @return the movie
	 */
	public Movie getMovie() {
		return movie;
	}

	/**
	 * @param movie the movie to set
	 */
	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	/**
	 * @return the movieId
	 */
	public int getMovieId() {
		return movieId;
	}

	/**
	 * @param movieId the movieId to set
	 */
	public void setMovieId(int movieId) {
		this.movieId = movieId;
	}

	public String ranking() throws Exception {
		return Constants.RANKING;
	}	
	

	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}


	/**
	 * @param movies the movies to set
	 */
	public void setMovies(List<Movie> movies) {
		this.movies = movies;
	}
	
	public List<Movie> getMovies() {
		return movies;
	}

	public void setCalendarId(int calendarId) {
		this.calendarId = calendarId;
	}


	/**
	 * @return the calendarId
	 */
	public int getCalendarId() {
		return calendarId;
	}

	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}


	public void setCountryId(int countryId) {
		this.countryId = countryId;
	}


	public void setPersonId(int personId) {
		this.personId = personId;
	}


	public void setAwardId(int awardId) {
		this.awardId = awardId;
	}

	
	public void setPaging(Paging paging) {
		this.paging = paging;
	}

	//@JSON(serialize=false)告诉json不需要转化这个属性,或者根本不写这个get方法,或者excludeproperties
	public Paging getPaging() {
		return paging;
	}

	public void setType(int type) {
		this.type = type;
	}


}
